<template>
    <el-dialog
            :title="!dataForm.orderId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="主键ID" prop="id">
                        <el-input v-model.number="dataForm.id" placeholder="主键ID"></el-input>
                </el-form-item>
                <el-form-item label="客户ID" prop="customerId">
                        <el-input v-model.number="dataForm.customerId" placeholder="客户ID"></el-input>
                </el-form-item>
                <el-form-item label="最后一次购买日期" prop="lastPurchaseDate">
                        <el-date-picker
                                v-model="dataForm.lastPurchaseDate"
                                type="datetime"
                                placeholder="最后一次购买日期">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="购买频率" prop="purchaseFrequency">
                        <el-input v-model.number="dataForm.purchaseFrequency" placeholder="购买频率"></el-input>
                </el-form-item>
                <el-form-item label="平均购买金额" prop="avgPurchaseAmount">
                        <el-input v-model="dataForm.avgPurchaseAmount" placeholder="平均购买金额"></el-input>
                </el-form-item>
                <el-form-item label="最后一次互动日期" prop="lastInteractionDate">
                        <el-date-picker
                                v-model="dataForm.lastInteractionDate"
                                type="datetime"
                                placeholder="最后一次互动日期">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="互动频率" prop="interactionFrequency">
                        <el-input v-model.number="dataForm.interactionFrequency" placeholder="互动频率"></el-input>
                </el-form-item>
                <el-form-item label="流失概率（0-1之间）" prop="churnProbability">
                        <el-input v-model="dataForm.churnProbability" placeholder="流失概率（0-1之间）"></el-input>
                </el-form-item>
                <el-form-item label="预测日期" prop="predictionDate">
                        <el-date-picker
                                v-model="dataForm.predictionDate"
                                type="datetime"
                                placeholder="预测日期">
                        </el-date-picker>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
        data () {
            return {
                visible: false,
                dataForm: {
                    id: null,
                    customerId: null,
                    lastPurchaseDate: null,
                    purchaseFrequency: null,
                    avgPurchaseAmount: null,
                    lastInteractionDate: null,
                    interactionFrequency: null,
                    churnProbability: null,
                    predictionDate: null,
                },
                dataRule: {
                    id: [
                        { required: true, message: '主键ID不能为空', trigger: 'blur' }
                    ],
                    customerId: [
                        { required: true, message: '客户ID不能为空', trigger: 'blur' }
                    ],
                    lastPurchaseDate: [
                        { required: true, message: '最后一次购买日期不能为空', trigger: 'blur' }
                    ],
                    purchaseFrequency: [
                        { required: true, message: '购买频率不能为空', trigger: 'blur' }
                    ],
                    avgPurchaseAmount: [
                        { required: true, message: '平均购买金额不能为空', trigger: 'blur' }
                    ],
                    lastInteractionDate: [
                        { required: true, message: '最后一次互动日期不能为空', trigger: 'blur' }
                    ],
                    interactionFrequency: [
                        { required: true, message: '互动频率不能为空', trigger: 'blur' }
                    ],
                    churnProbability: [
                        { required: true, message: '流失概率（0-1之间）不能为空', trigger: 'blur' }
                    ],
                    predictionDate: [
                        { required: true, message: '预测日期不能为空', trigger: 'blur' }
                    ],
                }
            }
        },
        methods: {
            init (id) {
                this.dataForm.orderId = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    this.dataForm.orderId = id || 0
                    if (this.dataForm.orderId) {
                        this.$http({
                            url: this.$http.adornUrl(`/app/AppCustomerChurn/info/\\$\\{this.dataForm.orderId}`),
                            method: 'get',
                            params: this.$http.adornParams()
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.dataForm = data.appcustomerchurn
                            }
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit () {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.$http({
                            url: this.$http.adornUrl(`/app/AppCustomerChurn/\\$\\{!this.dataForm.orderId ? 'save' : 'update'}`),
                            method: 'post',
                            data: this.$http.adornData(this.dataForm)
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.$message({
                                    message: '操作成功',
                                    type: 'success',
                                    duration: 1500,
                                    onClose: () => {
                                        this.visible = false
                                        this.$emit('refreshDataList')
                                    }
                                })
                            } else {
                                this.$message.error(data.msg)
                            }
                        })
                    }
                })
            }
        }
    }
</script>